package hu.rsc.feeds.server;

import hu.rsc.feeds.client.data.GeneratedEntry;
import hu.rsc.feeds.client.data.UserInfo;

import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.XSLTransformException;
import org.jdom.transform.XSLTransformer;

public class SendOfferMail {
    private GeneratedEntry entry;
    private UserInfo sender;
    List<String> recipients;

    private static final String MAIL_TITLE_EN = "New Channel is accesible.";
    private static final String MAIL_TITLE_HU = "Új csatorna érhető el.";

    XMLOutputter outputter;
    XSLTransformer transformer;
    OutputStream stream;

    public SendOfferMail(GeneratedEntry entry, UserInfo sender, List<String> recipinets, OutputStream stream) {
	this.entry = entry;
	this.sender = sender;
	this.stream = stream;
	this.recipients = recipinets;
	share();
    }

    public XMLOutputter getOutputter() {
	return this.outputter;
    }

    private void share() {
	Properties props = new Properties();
	props.setProperty("mail.mime.charset", "utf-8");
	Session session = Session.getDefaultInstance(props, null);

	Document doc = new Document();
	Element root = new Element("share");
	doc.setRootElement(root);

	Element senderEl = new Element("sender");
	senderEl.setText(this.sender.getUserName());
	root.addContent(senderEl);

	Element titleEl = new Element("title");
	titleEl.setText(this.entry.getTitle());
	root.addContent(titleEl);

	Element linkEl = new Element("description");
	linkEl.setText(this.entry.getDescription());
	root.addContent(linkEl);

	this.outputter = new XMLOutputter();

	try {
	    try {
		this.transformer = new XSLTransformer(this.sender.getUserName() + this.sender.getLang() == null
			|| this.sender.getLang().equals(UserInfo.LANG_EN) ? "new_feed_en.xsl" : "new_feed_hu.xsl");
	    } catch (XSLTransformException e1) {
		e1.printStackTrace();
	    }

	    if (this.stream == null) {
		String htmlBody = this.outputter.outputString(this.transformer.transform(doc));
		Message msg = new MimeMessage(session);
		String encodingOptions = "text/html; charset=\"UTF-8\"";
		msg.setHeader("Content-Type", encodingOptions);
		msg.setFrom(new InternetAddress(RssReaderServiceImpl.SENDER, RssReaderServiceImpl.APP_NAME));

		for (String str : this.recipients) {
		    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(str, "Recipient"));
		}
		msg.setSubject(this.sender.getUserName()
			+ (this.sender.getLang() == null || this.sender.getLang().equals(UserInfo.LANG_EN) ? MAIL_TITLE_EN : MAIL_TITLE_HU));

		Multipart mp = new MimeMultipart();

		MimeBodyPart htmlPart = new MimeBodyPart();
		htmlPart.setContent(htmlBody, "text/html");
		mp.addBodyPart(htmlPart);

		msg.setContent(mp);

		Transport.send(msg);
	    } else {
		this.outputter.output(this.transformer.transform(doc), this.stream);
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

}
